﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-5-11 13:14:22
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using DALFactory;
using Model.TaoBao;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace BLL.TaoBao
{
    /// <summary>
    /// 业务逻辑类 TB_BAOBEI
    /// </summary>
    public class TB_BAOBEI : RemoveCaches
    {
        private readonly IDAL.TaoBao.ITB_BAOBEI dal = DataAccess.CreateTaoBaoTB_BAOBEI();
        public TB_BAOBEI()
            : base("TaoBao_TB_BAOBEI_")
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.TaoBao.TB_BAOBEI model)
        {
            int data = dal.Add(model);
            if (Caches.EnableCache && data != 0)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
            return data;
        }

        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Model.TaoBao.TB_BAOBEI> l)
        {
            foreach (Model.TaoBao.TB_BAOBEI model in l)
                dal.Add(model);

            if (Caches.EnableCache)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaoBao.TB_BAOBEI model)
        {
            bool b = dal.Update(model);
            if (Caches.EnableCache && b)
            {
                RemoveModelCache(model.MID);
                RemoveListCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public void Update(List<Model.TaoBao.TB_BAOBEI> l)
        {
            foreach (Model.TaoBao.TB_BAOBEI model in l)
            {
                bool b = dal.Update(model);
                if (Caches.EnableCache && b)
                    RemoveModelCache(model.MID);
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MID)
        {
            bool b = dal.Delete(MID);
            if (Caches.EnableCache && b)
            {
                RemoveExistsCache(MID);
                RemoveModelCache(MID);
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public void Delete(List<int> l)
        {
            foreach (int MID in l)
            {
                bool b = dal.Delete(MID);
                if (Caches.EnableCache && b)
                {
                    RemoveExistsCache(MID);
                    RemoveModelCache(MID);
                }
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MID)
        {
            if (!Caches.EnableCache)
                return dal.Exists(MID);

            string key = basicKey + "Exists_" + MID.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (bool)HttpRuntime.Cache[key];
            else
            {
                bool data = dal.Exists(MID);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetTaoBaoTB_BAOBEICacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaoBao.TB_BAOBEI GetModel(int MID)
        {
            if (!Caches.EnableCache)
                return dal.GetModel(MID);

            string key = basicKey + "Model_" + MID.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (Model.TaoBao.TB_BAOBEI)HttpRuntime.Cache[key];
            else
            {
                Model.TaoBao.TB_BAOBEI data = dal.GetModel(MID);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetTaoBaoTB_BAOBEICacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), System.Web.Caching.CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 获得泛型数据列表
        /// </summary>
        public List<Model.TaoBao.TB_BAOBEI> GetAllList()
        {
            if (!Caches.EnableCache)
                return dal.GetList() as List<Model.TaoBao.TB_BAOBEI>;

            string key = basicKey + "List_All";
            if (HttpRuntime.Cache[key] != null)
                return (List<Model.TaoBao.TB_BAOBEI>)HttpRuntime.Cache[key];
            else
            {
                List<Model.TaoBao.TB_BAOBEI> data = dal.GetList() as List<Model.TaoBao.TB_BAOBEI>;
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetTaoBaoTB_BAOBEICacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 分页获取泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.TaoBao.TB_BAOBEI> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            if (!Caches.EnableCache)
                return dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.TaoBao.TB_BAOBEI>;

            string key = basicKey + "List_" + fldSort + "_" + sort.ToString() + "_" + strCondition + "_" + pageSize.ToString() + "_" + pageIndex.ToString();
            if (HttpRuntime.Cache[key] != null)
            {
                Model.PageData data = (Model.PageData)HttpRuntime.Cache[key];
                pageCount = data.PageCount;
                count = data.Count;
                return (List<Model.TaoBao.TB_BAOBEI>)data.Data;
            }
            else
            {
                List<Model.TaoBao.TB_BAOBEI> lst = dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.TaoBao.TB_BAOBEI>;
                Model.PageData data = new Model.PageData(lst, count, pageCount);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetTaoBaoTB_BAOBEICacheDependency();
                if (pageIndex <= 3)
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                else
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return lst;
            }
        }
        #endregion
    }
}
